// pages/article/article.js
import parser from 'rich-text-parser';
import {Article} from "../../model/article"
Page({
  data: {
    nodes: [],
    article: null,
    timecount: '00:00:00',
    idx: 0
  },
  timer: null,
  aid: null,
  second: 0,
  minute: 0,
  hour: 0,
  onLoad: async function (options) {
    this.eventChannel = this.getOpenerEventChannel()
    this.eventChannel.on('updateItem')

    let that = this
    that.aid = options.aid
    const article = await Article.getArticleDetail(options.aid)
    parser.definedCustomTag({figure: 'img', figcaption: ''})
    const contents = this.formatRichText(article.content)
    console.log(contents)
    const nodes = parser.getRichTextJson(contents)
    console.log(nodes)
    that.setData({
      idx: options.idx,
      article,
      nodes: nodes.children
    }, ()=> {
      that.start()
    })
  },
  formatRichText(html){
    let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    // newContent = newContent.replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
    newContent = newContent.replace(/\<p>/gi, '<p class="p_class">')

    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;margin-right:auto;margin-left:auto;margin-top:0;margin-bottom:0;"');
    return newContent;
  },
  start() {
    let that = this
    clearInterval(this.timer)
    that.second = 0
    that.minute = 0
    that.hour = 0
    this.timer = setInterval(function () {
      that.timerloop()
    }, 1000)
  },
  stop() {
    clearInterval(this.timer)
  },
  timerloop() {
    let that = this
    that.second++;
    if (that.second >= 60) {
      that.second = 0
      that.minute += 1
    }
    if (that.minute >= 60) {
      that.minute = 0
      that.hour += 1
    }
    that.setData({
      timecount: (that.hour<10?'0'+that.hour:that.hour) + ':' + (that.minute<10?'0'+that.minute:that.minute) + ':' + (that.second<10?'0'+that.second:that.second)
    })
  },
  onUnload: async function () {
    const that = this
    clearInterval(this.timer)
    let duration = this.hour * 60 * 60 + this.minute * 60 + this.second
    if (duration > 5) {
      if (this.data.article.cover === '视频') {
       if (duration > 15 * 60) {
         duration = 15*60
       }
      }else {
        if (duration > 5 * 60) {
          duration = 5*60
        }
      }
      const data = await Article.recordStudyTime(this.aid, duration)
      if (data) {
        that.eventChannel.emit('updateItem', {data: data.duration});
        that.eventChannel.off('updateItem');
      }
    }else {
      that.eventChannel.off('updateItem');
    }
  }
})
